﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Snowball;
using RapidWebDev.SearchIndex.Contract;

namespace RapidWebDev.SearchIndex.Analyzers
{
	/// <summary>
	/// Resolve Lucene Analyzer for tokenization.
	/// </summary>
	public class SnowballLuceneAnalyzerResolver : ILuceneAnalyzerResolver
	{
		/// <summary>
		/// Resolve an Lucene analyzer for the stemming language for tokenization.
		/// </summary>
		/// <param name="indexDocFieldValueType"></param>
		/// <param name="stemmingLanguage"></param>
		/// <returns></returns>
		public Analyzer Resolve(IndexDocFieldValueTypes indexDocFieldValueType, string stemmingLanguage)
		{
			switch (indexDocFieldValueType)
			{
				case IndexDocFieldValueTypes.Date:
				case IndexDocFieldValueTypes.DateTime:
				case IndexDocFieldValueTypes.Decimal:
				case IndexDocFieldValueTypes.Integer:
				case IndexDocFieldValueTypes.Keyword:
					return null;
				default:
					return ResolveSnowballAnalyzer(stemmingLanguage);
			}
		}

		private static SnowballAnalyzer ResolveSnowballAnalyzer(string stemmingLanguage)
		{
			switch (stemmingLanguage)
			{
				case "English":
				case "Danish":
				case "Dutch":
				case "Finnish":
				case "French":
				case "German2":
				case "German":
				case "Italian":
				case "Kp":
				case "Lovins":
				case "Norwegian":
				case "Porter":
				case "Portuguese":
				case "Russian":
				case "Spanish":
				case "Swedish":
					return new SnowballAnalyzer(stemmingLanguage);
				default:
					return new SnowballAnalyzer("English");
			}
		}
	}
}